/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.usersaction;

import android.text.TextUtils;

import com.alstudio.module.xmpp.protocol.xmpp.packact.ALXmppDomainConfig;
import com.alstudio.module.xmpp.protocol.xmpp.packact.IQNameSpace;
import com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.AlXmlTag;
import com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.BaseIQTemplate;
import com.alstudio.pojo.module.user.User;

/**
 * 修改个人资料IQ
 * 
 * @author alonso lee
 * 
 */
public class ModifyUserProfileIQCreator extends BaseIQTemplate {

	private User user;

	/**
	 * 
	 */
	public ModifyUserProfileIQCreator(User u, String domain) {
		// TODO Auto-generated constructor stub
		this.user = u;
		this.domain = domain;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.BaseIQTemplate
	 * #getIQXml()
	 */
	@Override
	public String getIQXml() {
		// TODO Auto-generated method stub
		mALIQCreator.setNs(IQNameSpace.NS_MODIFY_USER_PROFILE);
		mALIQCreator.setType(IQ_TYPE_SET);
		to = ALXmppDomainConfig.getALXmppDomainConfig().getDomainByNameSpace(
				mALIQCreator.getNs());
		mALIQCreator.setTo(to + domain);

		// vcard起始节点
		AlXmlTag tag = new AlXmlTag("vcard");
		tag.setHasSubTag(true);
		tagList.add(tag);

		// 修改昵称
		if (!TextUtils.isEmpty(user.getNick())) {
			tag = new AlXmlTag("nick");
			tag.setText(user.getNick());
			tagList.add(tag);
		}
		// 修改生日
		if (!TextUtils.isEmpty(user.getBirtyday())) {
			tag = new AlXmlTag("birthday");
			tag.setText(user.getBirtyday());
			tagList.add(tag);
		}

		// 修改签名
		if (!TextUtils.isEmpty(user.getSignature())) {
			tag = new AlXmlTag("sig");
			tag.setText(user.getSignature());
			tagList.add(tag);
		}

		// 修改省份
		if (!TextUtils.isEmpty(user.getProvince())) {
			tag = new AlXmlTag("province");
			tag.setText(user.getProvince());
			tagList.add(tag);
		}

		// 修改市区
		if (!TextUtils.isEmpty(user.getCity())) {
			tag = new AlXmlTag("city");
			tag.setText(user.getCity());
			tagList.add(tag);
		}

		// 修改相册
		if (user.getPhotos() != null && user.getPhotos().size() != 0) {
			tag = new AlXmlTag("photos");
			tag.setHasSubTag(true);
			tagList.add(tag);
			for (int i = 0; i < user.getPhotos().size(); i++) {
				if (user.getPhotos().get(i).getThumbnailFileid().length() == 0) {
					continue;
				}

				AlXmlTag photoTag = new AlXmlTag("photo");
				photoTag.setAtt("index", "" + i);
				// 缩略图fileid
				photoTag.setAtt("small_pic", ""
						+ user.getPhotos().get(i).getThumbnailFileid());
				// 大图fileid
				photoTag.setAtt("large_pic", ""
						+ user.getPhotos().get(i).getPicFileid());
				tagList.add(photoTag);

			}

		}

		String xml = "";
		try {
			xml = mALIQCreator.getXml();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xml;
	}

}
